<template>
  <div class="app-container">
    <!--filter-->
    <el-form :inline="true" :model="filters" ref="filters" class="demo-form-inline">
      <el-form-item label="会员代充时间" prop="time">
        <el-date-picker
          v-model="form.time"
          type="datetimerange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerDayStep">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="下级会员账户名" prop="orderType">
        <el-input v-model="filters.userName" @keyup.enter.native="submitFilterForm"/>
      </el-form-item>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
      <el-button v-if="roleCheck('export-member-record')" class="filter-item" type="primary" icon="el-icon-download" @click="exportHandle">导出</el-button>
      <el-button @click="cancel('filters')">重置</el-button>
    </el-form>

    <!--table-->
    <el-table
      class="el-table-align-center"
      :data="tableData.list"
      border
      v-loading="loading">
      <el-table-column label="会员代充时间">
        <template slot-scope="{row}">
          {{row.createTime | toTime }}
        </template>
      </el-table-column>
      <el-table-column label="下级会员账户名">
        <template slot-scope="{row}">
          {{row.username}}
        </template>
      </el-table-column>
      <el-table-column label="代充金额">
        <template slot-scope="{row}">
          {{row.amount | currency}}
        </template>
      </el-table-column>
      <el-table-column label="代充类型">
        <template slot-scope="{row}">
          {{row.type}}
          <span v-if="row.type === 0">授信账户</span>
          <span v-if="row.type === 1">代充账户</span>
        </template>
      </el-table-column>
      <el-table-column label="会员代充前" prop="creditedAmountBefore">
        <template slot-scope="{row}">
          {{row.balanceBefore | currency}}
        </template>
      </el-table-column>
      <el-table-column label="会员代充后" prop="creditedAmountAfter">
        <template slot-scope="{row}">
          {{row.balance | currency}}
        </template>
      </el-table-column>
    </el-table>
    <!--page-->
    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="tableData.total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { exportMembersAllRecordList, getMemberUpAmountList } from '@/services/api/member';
import { form, pickerOption, grid } from '@/build';

export default {
  name: 'member-record',
  mixins: [grid, form, pickerOption],
  props: ['rowData', 'member'],
  data() {
    return {
      localSummariesText: '当前页汇总 / 所有页汇总',
      tableData: {
        pageinfo: {},
      },
      filters: {
        page: 1,
        size: 20,
        parentId: this.$store.state.member.member_id,
        userName: undefined,
        parentName: this.member.username,
        status: -1,
        orderType: 11,
      },
      form: {
        time: [moment().startOf('month').format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD 23:59:59')],
      },
      filterDateType: false,
      statistic: [
        {
          index: 2,
          prop: 'creditedAmountBefore',
          format: value => <div> {_.currency(value)} /  {_.currency(this.tableData.proxyBeforeCount)}</div>,
        },
        {
          index: 3,
          prop: 'creditedAmountAfter',
          format: value => <div> {_.currency(value)} /  {_.currency(this.tableData.proxyAfterCount)}</div>,
        },
      ],
    };
  },
  methods: {
    initialFilter() {
      this.filters.page = 1;
      this.filters.userName = undefined;
      this.form.time = [moment().startOf('month').format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD 23:59:59')];
    },
    cancel(formName) {
      this.resetForm(formName);
      this.form = {
        time: [moment().startOf('month').format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD 23:59:59')],
      };
      this.initialFilter();
      this.submitFilterForm();
    },
    // 导出
    exportHandle() {
      const {
        parentId, userName, orderType, parentName,
      } = this.filters;
      const maintainTime = this.form.time;
      let startDate;
      let endDate;
      if (!_.isUndefined(maintainTime) && _.isArray(maintainTime)) {
        startDate = maintainTime[0];
        endDate = maintainTime[1];
      }
      const ajaxData = {
        parentId, startDate, endDate, userName, orderType, parentName,
      };
      const filename = '会员上分记录.csv';
      const header = ['会员上分时间', '下级会员账户名', '上分金额', '会员上分前', '会员上分后'];
      const filterField = ['createTime', 'username', 'changeAmount', 'creditedAmountBefore', 'creditedAmountAfter'];
      exportMembersAllRecordList(ajaxData).then(([result, data]) => {
        if (result) {
          const exportData = this.responseFormatter(data.list);
          this.export2Excel(header, filterField, exportData, filename);
        } else {
          this.$message({ message: `导出失败,${data.message}`, type: 'warning' });
        }
      });
    },
    responseFormatter(data) {
      _.forEach(data, (item) => {
        item.createTime = _.toTime(item.createTime);
        item.creditedAmountBefore = _.currency(item.creditedAmountBefore);
        item.creditedAmountAfter = _.currency(item.creditedAmountAfter);
        item.changeAmount = _.currency(item.changeAmount);
      });
      return data;
    },
    submitFilterForm() {
      this.gridSearch();
    },
  },

  mounted() {
    this.bindGet(getMemberUpAmountList, 'tableData', {
      action: 'getGrid',
      sendingData: 'filters',
      beforeSend: (data) => {
        const maintainTime = this.form.time;
        let startDate;
        let endDate;
        if (!_.isUndefined(maintainTime) && _.isArray(maintainTime)) {
          startDate = maintainTime[0];
          endDate = maintainTime[1];
          data = { ...this.filters, startDate, endDate };
        } else {
          data.startDate = undefined;
          data.endDate = undefined;
        }
        return data;
      },
    });
    this.getAll();
  },
};
</script>
